#ifndef __SPDF_OPT_H__
#define __SPDF_OPT_H__

#include "stdint.h"
#include "mytype.h"

#ifdef __cplusplus
extern "C"
{
#endif

#define spdf_sprintf sprintf

#define SPDF_OK 0
#define SPDF_FAILED 1
#define SPDF_TRUE 1
#define SPDF_FALSE 0
#define SPDF_NOERROR 0

#define SPDF_INVALID_OBJECT 0x1033
#define SPDF_INVALID_IMAGE 0x1030
#define SPDF_INVALID_COLOR_SPACE 0x1020
#define SPDF_INVALID_IMAGE_WIDTH 0x1031
#define SPDF_PAGE_INVALID_SIZE 0x1054
#define SPDF_INVALID_DOCUMENT 0x1025
#define SPDF_PAGE_INVALID_FONT 0x104F

#define SPDF_FONT_INVALID 0x00FF
#define SPDF_FONT_COURIER 0x0000
#define SPDF_FONT_COURIER_B 0x0001
#define SPDF_FONT_COURIER_O 0x0002
#define SPDF_FONT_COURIER_OB 0x0003
#define SPDF_FONT_HELVETICA 0x0004
#define SPDF_FONT_HELVETICA_B 0x0005
#define SPDF_FONT_HELVETICA_O 0x0006
#define SPDF_FONT_HELVETICA_BO 0x0007
#define SPDF_FONT_TIMES_R 0x0008
#define SPDF_FONT_TIMES_B 0x0009
#define SPDF_FONT_TIMES_I 0x000A
#define SPDF_FONT_TIMES_BI 0x000B
#define SPDF_FONT_SYMBOL 0x000C
#define SPDF_FONT_ZAP 0x000D

#define SPDF_GMODE_PAGE_DESCRIPTION 0x0001
#define SPDF_GMODE_PATH_OBJECT 0x0002
#define SPDF_GMODE_TEXT_OBJECT 0x0004
#define SPDF_GMODE_CLIPPING_PATH 0x0008
#define SPDF_GMODE_SHADING 0x0010
#define SPDF_GMODE_INLINE_IMAGE 0x0020
#define SPDF_GMODE_EXTERNAL_OBJECT 0x0040
#define SPDF_GMODE_INVALID 0x0080
#define SPDF_GMODE_OVER 0x0100

	typedef enum
	{
		SPDF_CS_DEVICE_GRAY = 0,
		SPDF_CS_DEVICE_RGB,
		SPDF_CS_DEVICE_CMYK,
		SPDF_CS_CAL_GRAY,
		SPDF_CS_CAL_RGB,
		SPDF_CS_LAB,
		SPDF_CS_ICC_BASED,
		SPDF_CS_SEPARATION,
		SPDF_CS_DEVICE_N,
		SPDF_CS_INDEXED,
		SPDF_CS_PATTERN,
		SPDF_CS_EOF
	} SPDF_ColorSpace;

	typedef enum
	{
		SPDF_OBJ_INFO = 0,
		SPDF_OBJ_STREAM,
		SPDF_OBJ_FONT,
		SPDF_OBJ_PAGE,
		SPDF_OBJ_CATALOG,
		SPDF_OBJ_PAGES,
		SPDF_OBJ_IMAGE,
		SPDF_OBJ_EOF
	} SPDF_ObjType;

	typedef enum
	{
		SPDF_PAGE_HEIGHT = 0,
		SPDF_PAGE_WIDTH,
		SPDF_PAGE_CTX_LENGTH,
		SPDF_PAGE_GMODE,
		SPDF_PAGE_FONTTYPE,
		SPDF_PAGE_FONTSIZE,
		SPDF_PAGE_EOF
	} SPDF_PageAttr;

	typedef enum
	{
		SPDF_IMAGE_HEIGHT = 0,
		SPDF_IMAGE_WIDTH,
		SPDF_IMAGE_BITS,
		SPDF_IMAGE_COLORSPACE,
		SPDF_IMAGE_LENGTH,
		SPDF_IMAGE_ADDR,
		SPDF_IMAGE_OWNER,
		SPDF_IMAGE_NUMBER,
		SPDF_IMAGE_EOF
	} SPDF_ImageAttr;

	typedef struct
	{
		SPDF_ObjType type;
		uu16 offset;
		uu16 number;
		int attr[10];
		void *doc;
	} SPDF_Obj;

	typedef struct
	{
		SPDF_Obj *obj;
		int offset;
		uu16 pagesNumber;
		uu16 objNumber;
		uu16 imageNumber;
		uu16 obj_len_max;
		uu32 error;

		char buf[512];

		struct
		{
			uint16_t file_buff_index;
			uint16_t file_buff_size;
			uint8_t *file_buff;
			int (*file_disk_out)(uint8_t *buff, int len);
		} fil;
	} SPDF_Doc_Rec;

	typedef SPDF_Doc_Rec *SPDF_Doc;
	typedef SPDF_Obj *SPDF_Page;
	typedef SPDF_Obj *SPDF_Image;
	extern const char *font_list[];

	const char *spdf_get_version(void);

	SPDF_Doc spdf_init(SPDF_Doc pdf, SPDF_Obj *pdf_obj, uu16 obj_len);
	void spdf_disk_out_link(SPDF_Doc pdf, int (*disk_out)(uint8_t *buff, int len));
	void spdf_disk_buff_set(SPDF_Doc pdf, uint8_t *buff, int len); 
	SPDF_Doc spdf_create(SPDF_Doc pdf_doc);

	void spdf_custom(SPDF_Page page, const char *cmd);

	void spdf_custom_with_par(
		SPDF_Page page,
		const char *cmd,
		ii32 par,
		uu8 ip_n);

#define SPDF_CMD_NoExe 0x01
#define SPDF_CMD_End 0x02
#define SPDF_CMD_NoNum 0x10
#define SPDF_CMD_TFore 0x20
	typedef struct
	{
		const char *text;
		ii16 val;
		uu8 ip_n;
		
		uu8 attr;
	} spdf_custom_par_type;

	void spdf_custom_cfg(
		spdf_custom_par_type *par,
		const char *text,
		ii16 val,
		uu8 ip_n,
		uu8 attr);

	void spdf_custom_exe(SPDF_Page page, spdf_custom_par_type *par, ii8 max);

	ii8 spdf_has_doc(SPDF_Doc pdf);

	SPDF_Page spdf_add_page(SPDF_Doc pdf);

	uu32 spdf_save(SPDF_Doc pdf, const char *file_name);

	uu32 spdf_page_check(SPDF_Page page, uu32 mode);

	uu32 spdf_page_save(SPDF_Page page);

	uu16 spdf_page_get_height(SPDF_Page page);

	uu16 spdf_page_get_width(SPDF_Page page);

	uu32 spdf_page_set_height(SPDF_Page page, uu16 value);

	uu32 spdf_page_set_width(SPDF_Page page, uu16 value);

	uu32 spdf_page_push(SPDF_Page page);
	uu32 spdf_page_pop(SPDF_Page page);

	uu32 spdf_page_set_font(SPDF_Page page, const char *font_name, uu8 size);

	uu32 spdf_page_begin_text(SPDF_Page page);

	uu32 spdf_page_set_text_rgb(SPDF_Page page, uu8 r, uu8 g, uu8 b);

	uu32 spdf_page_set_text_gray(SPDF_Page page, uu8 gray);

	uu32 spdf_page_set_text_mode(SPDF_Page page, uu8 mode, uu16 wide);

	uu32 spdf_page_show_text(SPDF_Page page, const char *text);

	uu32 spdf_page_show_text_check(SPDF_Page page);

	uu32 spdf_page_show_text_ascii(SPDF_Page page, const char *text);

	uu32 spdf_page_show_text_gbk(SPDF_Page page, const char *text);

	uu32 spdf_page_move_text_pos(SPDF_Page page, ii32 x, ii32 y);

	uu32 spdf_page_p1_move_text_pos(SPDF_Page page, ii32 x, ii32 y);

	uu32 spdf_page_text_out(
		SPDF_Page page,
		uu16 x, uu16 y,
		const char *text);

	uu32 spdf_page_end_text(SPDF_Page page);

	uu32 spdf_page_set_stroke_rgb(SPDF_Page page, uu8 r, uu8 g, uu8 b);

	uu32 spdf_page_set_stroke_gray(SPDF_Page page, uu8 gray);

	uu32 spdf_page_dash_off(SPDF_Page page);
	uu32 spdf_page_dash_on(SPDF_Page page, ii16 on, ii16 off, uu8 ip_n);

	uu32 spdf_page_endpoint_mode(SPDF_Page page, uu8 mode); 
	uu32 spdf_page_node_mode(SPDF_Page page, uu8 mode);		

	uu32 spdf_page_set_line_width(SPDF_Page page, uu16 line_width);

	uu32 spdf_page_stroke(SPDF_Page page);

	uu32 spdf_page_stroke_fill(SPDF_Page page);

	uu32 spdf_page_move_to(SPDF_Page page, ii32 x, ii32 y, uu8 ip_n);

	uu32 spdf_page_p1_move_to(SPDF_Page page, ii32 x, ii32 y);

	uu32 spdf_page_line_to(SPDF_Page page, ii32 x, ii32 y, uu8 ip_n);

	uu32 spdf_page_p1_line_to(SPDF_Page page, ii32 x, ii32 y);

	uu32 spdf_page_curve123_to(
		SPDF_Page page,
		ii32 x1, ii32 y1,
		ii32 x2, ii32 y2,
		ii32 x3, ii32 y3,
		uu8 ip_n);

	uu32 spdf_page_curve23_to(
		SPDF_Page page,
		ii32 x2, ii32 y2,
		ii32 x3, ii32 y3,
		uu8 ip_n);

	uu32 spdf_page_curve13_to(
		SPDF_Page page,
		ii32 x1, ii32 y1,
		ii32 x3, ii32 y3,
		uu8 ip_n);

	uu32 spdf_page_cycle(
		SPDF_Page page,
		ii32 x, ii32 y,
		ii32 c,
		uu8 ip_n);

	uu32 spdf_page_rectangle(
		SPDF_Page page,
		ii32 x, ii32 y,
		ii32 width, ii32 height,
		uu8 ip_n);

	uu32 spdf_page_draw_img(SPDF_Page page,
							SPDF_Image image,
							uu16 x, uu16 y,
							uu16 width, uu16 height);

	SPDF_Image spdf_page_draw_img_from_file(SPDF_Doc pdf,
											const char *filename,
											uu32 width, uu32 height,
											SPDF_ColorSpace color_space);

	SPDF_Image spdf_page_draw_img_from_mem(SPDF_Doc pdf,
										   const uu8 *buf,
										   uu32 width, uu32 height,
										   SPDF_ColorSpace color_space,
										   uu32 bits_per_component);

	uu8 spdf_rgb_to_gray(uu8 *rgb);

	uu8 spdf_gry_to_rgb(uu8 *rgb, uu8 gray);

#ifdef __cplusplus
}
#endif

#endif
